package com.smiier.skin;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import cn.o.app.json.JsonUtil;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.skinapp.R;

import com.evan.common.adapter.BaseListAdapter;
import com.evan.common.utils.CacheCommon;
import com.evan.common.utils.CommonDBUtility;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.QuestionGetTask;
import com.smiier.skin.net.QuestionGetTask.QuestionGetRequest;
import com.smiier.skin.net.QuestionGetTask.QuestionGetResponse;
import com.smiier.skin.net.entity.QuestionAnswer;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.smiier.skin.utils.DBUtil;
import com.smiier.skin.vo.TipMessageVo;
import com.umeng.analytics.MobclickAgent;

/**
 * @{# TipMessageActivity.java Create on 2014年10月22日 下午3:24:48
 * 
 * @author <a href="mailto:evan0502@qq.com">Evan</a>
 * @version 1.0
 * @description
 * 
 */
public class TipMessageActivity extends BasicActivity implements OnItemClickListener{
	private ListView mListTipMessage;
	private TextView mTextEmpty;
	DBUtil dbUtil;
	TipMessageAdapter adapter;
	
	private ArrayList<TipMessageVo> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tip_message);
		

		mListTipMessage = (ListView) findViewById(R.id.list_tip_message);
		mTextEmpty = (TextView) findViewById(R.id.text_empty);

		init();

		setNavTitle("提醒消息");
		setNavRightBtn("清空");

		if (dbUtil == null) {
			dbUtil = DBUtil.getInstance(activity);
		}

		list   = new ArrayList<TipMessageVo>();
		 
		 
	    ArrayList<TipMessageVo>	 data = dbUtil.query(TipMessageVo.class);
	    
	    
		 for(int i = data.size() - 1; i >= 0 ; i--){
			 list.add(data.get(i));
		 }
		
		 
		 Log.i("testtip", "tipmessage.onCreate.data.size:" + data.size());
		
		if (list.size() < 1) {
			mTextEmpty.setVisibility(View.VISIBLE);
			mTextEmpty.setText("暂无提醒消息。");
		} else {
			mTextEmpty.setVisibility(View.GONE);
			adapter = new TipMessageAdapter(activity, list);
			mListTipMessage.setAdapter(adapter);
		}
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				for(int i = 1; i <= list.size(); i++){
					
					ContentValues values = new ContentValues();
					values.put("readState", 1);
					
					dbUtil.updata(TipMessageVo.class.getSimpleName(), values, "id=?",
							new String[] { i + "" });
					
					Intent intent = new Intent();
					intent.setAction(Constant.RECIVER_MYIFNO_TAB_NOREAD);
					intent.putExtra("show", false);
					getContext().sendBroadcast(intent);
					
				}
				
				
				refreshView();
			}
		}).start();
		
		mListTipMessage.setOnItemClickListener(this);
	}

	private void refreshView() {
		Intent noInfo = new Intent();
		noInfo.setAction(Constant.RECIVER_MYIFNO_TAB_NOREAD);
		noInfo.putExtra("show", false);

	   this.sendBroadcast(noInfo);
	   
	   Intent noItem = new Intent();
	   noItem.putExtra("show", false);
	   noItem.setAction(Constant.RECIVER_MYIFNO_ITEM_NOREAD);
	   this.sendBroadcast(noItem);
		
	}

	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.text_right) {
			dbUtil.delete(TipMessageVo.class, null, null);
			if (!CommonUtility.isNull(adapter)) {
				adapter.clear();
			}
		}
	}

	@SuppressLint("InflateParams")
	public static class TipMessageAdapter extends BaseListAdapter {

		public TipMessageAdapter(Activity activity, ArrayList<?> list) {
			super(activity, list);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (CommonUtility.isNull(convertView)) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.item_tip_message, null);
				holder.textDate = (TextView) convertView
						.findViewById(R.id.text_tip_date);
				holder.textContent = (TextView) convertView
						.findViewById(R.id.text_tip_content);
				CommonUtility.setViewHolderTag(convertView, holder);
			} else {
				holder = (ViewHolder) CommonUtility
						.getViewHolderTag(convertView);
			}

			TipMessageVo vo = (TipMessageVo) getItem(position);
			if (vo.time != null) {
				holder.textDate.setText(vo.time.toString());
			}
			holder.textContent.setText(vo.message);
			return convertView;
		}

		class ViewHolder {
			TextView textDate, textContent;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
		/**
		 * Intent intent = new Intent(activity,
								QuestionDetailActivity.class);
						intent.putExtra(Constant.IDENTITY_KEY, all.toString());
						activity.startActivity(intent);
						
		 */
		
		Log.i("testtip", "onItem.data:" + (list.get(position).mId));
		
		Log.i("testtip", "onItem.data:" + (list.get(position).message));
		
		
		/**
		 * 	QuestionGetTask task = new QuestionGetTask();
			QuestionGetRequest req = new QuestionGetRequest();
			req.qids = new ArrayList<Long>();
			req.qids.add(qid);
			req.token = GlobalSettings.sToken;
			task.addListener(new NetTaskListener<QuestionGetTask.QuestionGetRequest, QuestionGetTask.QuestionGetResponse>() {

				@Override
				public void onComplete(
						INetTask<QuestionGetRequest, QuestionGetResponse> task,
						QuestionGetResponse response) {
					if (response != null && response.Res.size() > 0) {
						QuestionAnswer answer = response.Res.get(0);

						Intent intent = new Intent(getContext(),
								ChatActivity.class);
						try {
							intent.putExtra(Constant.IDENTITY_KEY, JsonUtil
									.convert(answer).toString());
							startActivity(intent);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}

				@Override
				public void onException(
						INetTask<QuestionGetRequest, QuestionGetResponse> task,
						Exception e) {
					e.printStackTrace();
				}
			});
			task.setRequest(req);
			add(task);
		 */
		
		
		
	}
}
